using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace cjx.http
{
	public class httpSocket
	{
		public string GetPage(string url, string cookies, Encoding enc)
		{
			Uri uri = new Uri(url);
			string text = string.Empty;
			IPHostEntry hostEntry = Dns.GetHostEntry(uri.Host);
			IPAddress address = hostEntry.AddressList[0];
			IPEndPoint remoteEP = new IPEndPoint(address, 80);
			Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
			try
			{
				socket.Connect(remoteEP);
			}
			catch
			{
			}
			string text2 = "GET " + uri.PathAndQuery + " HTTP/1.1\r\n";
			text2 += "Connection:close\r\n";
			text2 += "Content-Type: application/x-www-form-urlencoded\r\n";
			text2 = text2 + "Host:" + uri.Host + "\r\n";
			if (!string.IsNullOrEmpty(cookies))
			{
				text2 = text2 + "Cookie:" + cookies + "\r\n\r\n";
			}
			byte[] bytes = enc.GetBytes(text2);
			socket.Send(bytes);
			byte[] array = new byte[1024];
			int num;
			do
			{
				num = socket.Receive(array);
				text += enc.GetString(array, 0, num);
			}
			while (num != 0);
			return text;
		}
		public string PostData(string postURL, string postString, string encoding)
		{
			Uri uri = new Uri(postURL);
			string text = "POST {0} HTTP/1.1\r\n";
			text += "Host: {1}\r\n";
			text += "User-Agent:Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0\r\n";
			text += "Content-Type:application/x-www-form-urlencoded\r\n";
			text += "Content-Length:{2}\r\n";
			text += "Connection:close\r\n\r\n";
			text += "{3}\r\n";
			text = string.Format(text, new object[]
			{
				uri.PathAndQuery,
				uri.Host,
				postString.Length,
				postString
			});
			byte[] bytes = Encoding.GetEncoding(encoding).GetBytes(text);
			IPAddress address = Dns.GetHostEntry(uri.Host).AddressList[0];
			IPEndPoint remoteEP = new IPEndPoint(address, 80);
			Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
			socket.Connect(remoteEP);
			if (!socket.Connected)
			{
			}
			socket.Send(bytes, bytes.Length, SocketFlags.None);
			return httpSocket.Recv(socket, Encoding.GetEncoding(encoding));
		}
		public static string Recv(Socket sock, Encoding encode)
		{
			byte[] array = new byte[1024];
			StringBuilder stringBuilder = new StringBuilder();
			Thread.Sleep(50);
			int count = sock.Receive(array);
			stringBuilder.Append(encode.GetString(array, 0, count));
			while (sock.Available > 0)
			{
				Thread.Sleep(300);
				Array.Clear(array, 0, array.Length);
				count = sock.Receive(array);
				stringBuilder.Append(encode.GetString(array, 0, count));
				encode.GetString(array, 0, count);
			}
			sock.Close();
			return stringBuilder.ToString();
		}
		private string method_0(string string_0, string string_1, string string_2)
		{
			string str = "----------------------------" + DateTime.Now.Ticks.ToString("x");
			HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(string_0);
			httpWebRequest.ContentType = "multipart/form-data; boundary=" + str;
			httpWebRequest.Method = "POST";
			httpWebRequest.KeepAlive = true;
			httpWebRequest.Credentials = CredentialCache.DefaultCredentials;
			byte[] bytes = Encoding.ASCII.GetBytes("\r\n--" + str + "\r\n");
			string format = "Content-Disposition: form-data; name=\"{0}\";filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";
			string s = string.Format(format, "file", string_1);
			byte[] bytes2 = Encoding.GetEncoding(string_2).GetBytes(s);
			httpWebRequest.ContentLength = new FileInfo(string_1).Length + (long)bytes2.Length + (long)(bytes.Length * 2) + 2L;
			Stream requestStream = httpWebRequest.GetRequestStream();
			requestStream.Write(bytes, 0, bytes.Length);
			requestStream.Write(bytes2, 0, bytes2.Length);
			FileStream fileStream = new FileStream(string_1, FileMode.Open, FileAccess.Read);
			byte[] array = new byte[4096];
			int count;
			while ((count = fileStream.Read(array, 0, array.Length)) != 0)
			{
				requestStream.Write(array, 0, count);
				requestStream.Flush();
			}
			bytes = Encoding.GetEncoding(string_2).GetBytes("\r\n--" + str + "--\r\n");
			requestStream.Write(bytes, 0, bytes.Length);
			requestStream.Close();
			fileStream.Close();
			WebResponse response = httpWebRequest.GetResponse();
			Stream responseStream = response.GetResponseStream();
			StreamReader streamReader = new StreamReader(responseStream);
			string result = streamReader.ReadToEnd();
			response.Close();
			return result;
		}
		public string GetCookies(string html, out string location)
		{
			StringBuilder stringBuilder = new StringBuilder();
			location = string.Empty;
			string[] array = html.Split(new string[]
			{
				"\r\n"
			}, StringSplitOptions.RemoveEmptyEntries);
			string[] array2 = array;
			for (int i = 0; i < array2.Length; i++)
			{
				string text = array2[i];
				if (text.StartsWith("Set-Cookie: "))
				{
					int num = text.IndexOf(";");
					string value = text.Substring(12, num - 11);
					stringBuilder.Append(value);
				}
				if (text.StartsWith("Location:"))
				{
					location = text.Substring(10);
				}
			}
			return stringBuilder.ToString();
		}
	}
}
